﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using Microsoft.Office.Server.ActivityFeed;

namespace MS.InformationManagement.BLL.GlobalCommands
{
    public class RemoveCapturedAndWeighUpDocuments :IBaseCommand
    {

        #region Fields

        EnvíoDeCorrespondenciaDTO[] _deliveries;

        #endregion

        #region Constructors

        public RemoveCapturedAndWeighUpDocuments(EnvíoDeCorrespondenciaDTO[] deliveries)
        {
            _deliveries = deliveries;
        }

        #endregion

        #region Implemented Command Methods

        public void Execute()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        SPList currentList = currentWeb.Lists[GlobalConstants.LIST_DOCUMENTOS_VENTANILLA];

                        foreach (var delivery in _deliveries)
                        {
                            if(!delivery.IsCopy && ShouldBeDeleted(delivery, currentWeb))
                            {
                                SPListItem currentItem = currentList.GetItemById(delivery.ListItemId);
                                currentWeb.Delete();
                                currentList.Update();
                            }
                        }
                    }
                }
            });     
        }

        public void Undo()
        {
        }

        #endregion

        #region Private Methods

        private bool ShouldBeDeleted(EnvíoDeCorrespondenciaDTO delivery, SPWeb currentWeb)
        {
            SPFolder selectedDocument = currentWeb.Lists[delivery.LibraryName].GetItemById(delivery.DocumentId).Folder;
            bool statusDocumentCapture = selectedDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_CAPTURED] == null ? false : (bool)selectedDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_CAPTURED];
            bool statusDocumentWeighUp = selectedDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_WEIGHT] == null ? false : (bool)selectedDocument.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_WEIGHT];

            if (!statusDocumentCapture || !statusDocumentWeighUp)
                return false;

            return allCopiesWeighUp(selectedDocument, delivery, currentWeb);
        }

        private bool allCopiesWeighUp(SPFolder currentFolder, EnvíoDeCorrespondenciaDTO delivery, SPWeb currentWeb)
        {
            SPQuery query = new SPQuery();
            query.Folder = currentFolder;
            query.Query = string.Format("<Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>{0}</Value></Eq></Where>", GlobalConstants.CTTN_COPIAS_RADICADO);
            SPListItemCollection copiesItem = currentWeb.Lists[delivery.LibraryName].GetItems(query);

            foreach(SPListItem item in copiesItem)
            {
                if (item[CustomSiteColumns.FLD_COPY_PESADO] == null || !((bool)item[CustomSiteColumns.FLD_COPY_PESADO]))
                    return false;
            }

            return true;
        }

        #endregion


    }
}
